package mx.com.matersys.vacs.web;

import java.util.ArrayList;
import java.util.List;

import mx.com.matersys.vacs.domain.Paciente;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RooWebScaffold(path = "pacientes", formBackingObject = Paciente.class)
@RequestMapping("/pacientes")
@Controller
public class PacienteController {
	
	
	@RequestMapping(value = "/asynSearchLastName", method = RequestMethod.POST, headers = "Accept=application/json")
	@ResponseBody
	public ResponseEntity<String> searchByLastName(
			@RequestParam(value = "apPaterno", required = false) String apPaterno) {
		
		List<Paciente> listP;
		//String apPaterno;
		
		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type", "application/json");
		headers.add("Content-Type", "application/text; charset=utf-8");
		
		
		if (apPaterno == null) {
			listP = new ArrayList<Paciente>();
		} else {
			listP = Paciente.findPacienteLikeLastName("%" + apPaterno + "%");
		}
		
		return new ResponseEntity<String>(Paciente.toJsonArray(listP), headers,
				HttpStatus.OK);
	}
}
